package com.ruoyi.bms.service.impl;

import com.ruoyi.bms.domain.BmsBooks;
import com.ruoyi.bms.domain.BmsUserUpload;
import com.ruoyi.bms.domain.BmsUserUploadVO;
import com.ruoyi.bms.mapper.BmsBooksMapper;
import com.ruoyi.bms.mapper.BmsUserUploadMapper;
import com.ruoyi.bms.service.IBmsUserUploadService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户上传管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-12
 */
@Service
@Slf4j
public class BmsUserUploadServiceImpl implements IBmsUserUploadService
{
    @Autowired
    private BmsUserUploadMapper brsUserUploadMapper;
    @Autowired
    private BmsBooksMapper bmsBooksMapper;

    /**
     * 查询用户上传管理
     * 
     * @param id 用户上传管理主键
     * @return 用户上传管理
     */
    @Override
    public BmsUserUpload selectBrsUserUploadById(Long id)
    {
        return brsUserUploadMapper.selectBrsUserUploadById(id);
    }

    /**
     * 查询用户上传管理列表
     *
     * @param bmsUserUpload 用户上传管理
     * @return 用户上传管理
     */
    @Override
    public List<BmsUserUpload> selectBrsUserUploadList(BmsUserUpload bmsUserUpload)
    {
        return brsUserUploadMapper.selectBrsUserUploadList(bmsUserUpload);
    }

    /**
     * 新增用户上传管理
     * 
     * @param bmsUserUpload 用户上传管理
     * @return 结果
     */
    @Override
    public int insertBrsUserUpload(BmsUserUpload bmsUserUpload)
    {
        bmsUserUpload.setCreateTime(DateUtils.getNowDate());
        return brsUserUploadMapper.insertBrsUserUpload(bmsUserUpload);
    }

    /**
     * 修改用户上传管理
     * 
     * @param bmsUserUpload 用户上传管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBrsUserUpload(BmsUserUpload bmsUserUpload)
    {
        //查询之前的用户订单作对比
        BmsUserUpload bmsUserUpload1 = brsUserUploadMapper.selectBrsUserUploadById(bmsUserUpload.getId());
        if(bmsUserUpload.getStatus() == 1 && bmsUserUpload1.getStatus() == 1 ) return 0;
        BmsBooks bmsBooks = bmsBooksMapper.selectBmsBooksById(bmsUserUpload1.getBookId());
        bmsUserUpload.setUpdateTime(DateUtils.getNowDate());
        bmsBooks.setBookStatus(bmsUserUpload.getStatus().toString());
        bmsBooksMapper.updateBmsBooks(bmsBooks);
        return brsUserUploadMapper.updateBrsUserUpload(bmsUserUpload);
    }

    /**
     * 批量删除用户上传管理
     * 
     * @param ids 需要删除的用户上传管理主键
     * @return 结果
     */
    @Override
    public int deleteBrsUserUploadByIds(Long[] ids)
    {
        return brsUserUploadMapper.deleteBrsUserUploadByIds(ids);
    }

    /**
     * 删除用户上传管理信息
     * 
     * @param id 用户上传管理主键
     * @return 结果
     */
    @Override
    public int deleteBrsUserUploadById(Long id)
    {
        return brsUserUploadMapper.deleteBrsUserUploadById(id);
    }

    /**
     * 获取用户上传列表 + 内容
     * @param bmsUserUpload
     * @return
     */
    @Override
    public List<BmsUserUploadVO> selectBrsUserUploadListMS(BmsUserUpload bmsUserUpload) {
        //获取用户上传列表
        List<BmsUserUpload> bmsUserUploads = selectBrsUserUploadList(bmsUserUpload);

        //获取书籍列表
        List<Long> collect = bmsUserUploads.stream().map(BmsUserUpload::getBookId).collect(Collectors.toList());
        List<BmsBooks> books = bmsBooksMapper.selectBmsBooksListByIdList(collect);
        books.sort(Comparator.comparingLong(BmsBooks::getId));
        bmsUserUploads.sort(Comparator.comparingLong(BmsUserUpload::getBookId));

        //封装数据
        List<BmsUserUploadVO> BmsUserUploadVOs = new ArrayList<>();
        for (int i = 0; i < bmsUserUploads.size(); i++) {
                    BmsUserUploadVO bmsUserUploadVO = BmsUserUploadVO.builder()
                            .id(bmsUserUploads.get(i).getId())
                            .userId(bmsUserUploads.get(i).getUserId())
                            .bookId(bmsUserUploads.get(i).getBookId())
                            .status(bmsUserUploads.get(i).getStatus())
                            .bookName(books.get(i).getBookName())
                            .content(books.get(i).getContent())
                            .build();
            BmsUserUploadVOs.add(bmsUserUploadVO);
        }
        BmsUserUploadVOs.sort(Comparator.comparingLong(BmsUserUploadVO::getId));
        return BmsUserUploadVOs;
    }

    @Override
    public List<BmsBooks> getMylistBookManagement() {
        Long userId = SecurityUtils.getUserId();
        List<BmsUserUpload> bmsUserUploads =brsUserUploadMapper.selectBrsUserUploadByUserId(userId);
        List<Long> collect = bmsUserUploads.stream().map(BmsUserUpload::getBookId).collect(Collectors.toList());
        List<BmsBooks> bmsBooks = bmsBooksMapper.selectBmsBooksListByIdList(collect);
        return bmsBooks;
    }
}
